<?php

namespace app\admin\controller;

use app\common\model\Project as ProjectModel;
use app\common\model\Users;
use think\facade\Request;
use think\facade\View;

class User extends Base
{
    //
    public function index(){
        $where   = [];
        $keyword = Request::param('keyword');
        if (!empty($keyword)) {
            $where[] = ['name', 'like', '%' . $keyword . '%'];
        }
        $dateran = Request::param('dateran');
        if (!empty($dateran)) {
            $getDateran = get_dateran($dateran);
            $where[]    = ['create_time', 'between', $getDateran];
        }
        $list = Users::getList($where, $this->pageSize, ['id' => 'desc']);
        $all_articleNum = model('users')->select();
        $view = [
            'keyword'  => $keyword,
            'dateran'  => $dateran,
            'pageSize' => page_size($this->pageSize, $list->total()),
            'page'     => $list->render(),
            'list'     => $list,
            'empty'    => empty_list(9),
            'all_order_num' => $all_articleNum
        ];
        View::assign($view);
        return view();
    }
    public function add(){
        if(request()->isAjax()){
            $result = model('users')->save(input('post.'));
            if($result)
            {
                $this->success('成功',"admin/project/index");
            }else{
                $this->error('失败!',$_SERVER['HTTP_REFERER']);
            }
        }
        return view();
    }
    public function edit($id=0){
        if(request()->isAjax()){
            $data = input('post.');
            $result = model('users')->where('id',$id)->update($data);
            if($result)
            {
                $this->success('修改成功',"admin/user/index");
            }else{
                $this->error('修改失败!',$_SERVER['HTTP_REFERER']);
            }
        }
        $Info = model('users')->find($id);
        $viewData = [
            'Info'=>$Info
        ];
        $this->assign($viewData);
        return view();
    }
    public function del()
    {
        $id = Request::param('id');
        Users::destroy($id);
        return json(['error' => 0, 'msg' => '删除成功']);
    }

    //批量删除
    public function selectDel()
    {
        $id = Request::param('id');
        Users::destroy($id);
        return json(['error' => 0, 'msg' => '删除成功!']);
    }

    //用户已经报名的项目
    public function enlist($id){
        $projects = model('apply')->where('user_id',$id)->field('project_id')->select()->toArray();
        $project_id_list = [];
        if($projects){
            foreach ($projects as $projects_val){
                array_push($project_id_list,$projects_val['project_id']);
            }
        }
        $project_info = model('project')->where('id','in',$project_id_list)->field('id,title')->select();
        $this->assign(compact('project_info'));
        return view();
    }
}
